package com.far.game.xiuzhen.component.self;


import com.far.game.xiuzhen.common.annot.RequestFarParam;
import com.far.game.xiuzhen.common.dto.TokenParam;
import com.far.game.xiuzhen.common.utils.RequestUtils;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/*
 * @description: 参数解析类 springboot专用的
 * @author mike/Fang.J/Q184377367
 * @date 2022/4/12 1:28
 */
public class ParamsHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    //判定解析的注解，可以配置支持多个注解，这里只配置了一个
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        if (methodParameter.hasParameterAnnotation(RequestFarParam.class))
            return true;
        return false;
    }

    //处理方法，如果有多个注解要区分开
    //if(parameter.getParameterAnnotation(Info.class) != null)
    //此处逻辑为获得请求header中的channel字段，映射generalRequest中的channel字段
    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        String token = RequestUtils.getToken(request);
        TokenParam tokenParam = RequestUtils.getTokenParam(token);
        return tokenParam;
    }


}
